package breadbox.client.files;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.Deflater;
import java.util.zip.DeflaterInputStream;

/**
 * This class takes care of the file input
 * @author Forrest
 *
 */
public class FileInput {
	protected File f;
	protected FileInputStream fileIn;
	protected DeflaterInputStream deflateIn;
	
	/**
	 * Construct a new FileInput
	 * @param filename String
	 * @param compression boolean
	 */
	public FileInput(String filename, boolean compression) {
		deflateIn = null;
		f = new File(filename);
		try {
			fileIn = new FileInputStream(f);
			if (compression) {
				// Compressor with highest level of compression
			    Deflater def = new Deflater();
			    def.setLevel(Deflater.BEST_COMPRESSION);
				deflateIn = new DeflaterInputStream(fileIn, def);
			}
		} catch (FileNotFoundException e) {
			System.err.println("File not found: " + f);
		}
	}
	
	/**
	 * Get the current input stream from the file
	 * @return InputStream dataIn
	 */
	public InputStream getData() {
		if (deflateIn != null) {
			return deflateIn;
		} else {
			return fileIn;
		}
	}
	
	/**
	 * Get the filename for the current file
	 * @return String filename
	 */
	public String getFilename() {
		return f.getName();
	}
	
	/**
	 * Close the current LoadFile
	 */
	public void close() {
		try {
			fileIn.close();
		} catch (IOException e) {
			System.err.println("Unable to close file stream");
		}
	}
	
}
